﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CAP.Forms
{
    public partial class EIGStopNodeData : UserControl
    {
        #region Attribute
        private DistributedGraph.EIGStop.AlgorithmController algorithm;
        #endregion Attribute

        #region Eigenschaften
        /// <summary>
        /// Setzt den AlgorithmusController für das Control.
        /// </summary>
        public DistributedGraph.EIGStop.AlgorithmController Algorithm
        {
            set
            {
                algorithm = value;
                // ToDo: Events in Algorithm und Algorithm.Nodes hier defnieren.
            }
        }
        #endregion Eigenschaften

        #region Methoden
        /// <summary>
        /// Instanziert und initialisiert das Control.
        /// </summary>
        public EIGStopNodeData()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Aktualisiert das Control unter Zuhilfenahme des markierten Knotens.
        /// </summary>
        /// <param name="selectedNode">Der markierte Knoten</param>
        public void Update(DistributedGraph.EIGStop.Node selectedNode)
        {
            updateTree(selectedNode);
            updateRound(selectedNode);
            updateConsens(selectedNode);

            tvEIGStop.ForeColor = selectedNode != null && selectedNode.FailStops ? Color.Red : Color.Black;
            tvNodesRound.ForeColor = selectedNode != null && selectedNode.FailStops ? Color.Red : Color.Black;
            lblConsensSuccess.ForeColor = selectedNode != null && selectedNode.FailStops ? Color.Red : Color.Black;
            lblDecisionSet.ForeColor = selectedNode != null && selectedNode.FailStops ? Color.Red : Color.Black;
            lblConsensSuccessDesc.ForeColor = selectedNode != null && selectedNode.FailStops ? Color.Red : Color.Black;
            lblDecisionSetDesc.ForeColor = selectedNode != null && selectedNode.FailStops ? Color.Red : Color.Black;
        }

        /// <summary>
        /// Aktualisiert den EIGBaum.
        /// </summary>
        /// <param name="selectedNode">Der markierte Knoten</param>
        private void updateTree(DistributedGraph.EIGStop.Node selectedNode)
        {
            tvEIGStop.Nodes.Clear();
            if (selectedNode != null)
            {
                updateTree(selectedNode.Tree, tvEIGStop.Nodes);
            }
        }

        /// <summary>
        /// Befüllt die Baumstruktur des EIGBaums
        /// </summary>
        /// <param name="tn">Treenode Struktur</param>
        /// <param name="tnc">Baumknoten</param>
        private void updateTree(DistributedGraph.EIGStop.TreeNode tn, TreeNodeCollection tnc)
        {
            TreeNode newNode = tnc.Add(tn.ToString());
            foreach (DistributedGraph.EIGStop.TreeNode tns in tn.Children)
            {
                updateTree(tns, newNode.Nodes);
            }
        }

        /// <summary>
        /// Aktualisiert die Daten der EIG Runde
        /// </summary>
        /// <param name="selectedNode">Der markierte Knoten</param>
        private void updateRound(DistributedGraph.EIGStop.Node selectedNode)
        {
            tvNodesRound.Nodes.Clear();
            if (selectedNode != null)
            {
                foreach (KeyValuePair<DistributedGraph.EIGStop.Node, List<DistributedGraph.EIGStop.TreeNode>> kvp in selectedNode.NodesThisRound)
                {
                    TreeNode newNode = tvNodesRound.Nodes.Add("Knoten: " + kvp.Key.Id.ToString());
                    foreach (DistributedGraph.EIGStop.TreeNode tn in kvp.Value)
                    {
                        newNode.Nodes.Add(tn.ToString());
                    }
                }
            }
        }

        /// <summary>
        /// Aktualisiert die Daten über den Konsens.
        /// </summary>
        /// <param name="selectedNode">Der markierte Knoten</param>
        private void updateConsens(DistributedGraph.EIGStop.Node selectedNode)
        {
            if (selectedNode != null)
            {
                lblConsensSuccess.Text = selectedNode.IsConsensReached ? "Ja" : "Nein";
                string menge = "";
                foreach (int i in selectedNode.DecisionResultSet) menge += (menge.Length != 0 ? ", " : "") + i.ToString();
                lblDecisionSet.Text = menge.Length == 0 ? "Ø" : "" + menge;
                lblIncorrectDecision.Visible = !selectedNode.IsDecisionCorrect;
            }
            else
            {
                lblConsensSuccess.Text = "-";
                lblDecisionSet.Text = "-";
                lblIncorrectDecision.Visible = false;
            }
        }
        #endregion Methoden
    }
}
